package mess;

public class LeeCode_55 {
    public static void main(String[] args) {
        int[] nums = new int[]{2,3,1,1,4};
        int[] nums2 = new int[]{2,0};
//        System.out.println(canJump(nums));
        System.out.println(canJump(nums2));
    }
    public static boolean canJump(int[] nums) {
        int len = nums.length;

        int index = 0;
        int max = 0;
        int maxIndex = 0;
        int distance = len - 1 - index;
        while (distance > 0){
            if (nums[index] > distance)
                return true;
            if (nums[index] == 0)
                return false;
            max = 0;
            int step = nums[index];
            for (int i = 1; i <= step; i++) {
                int go = i + nums[i + index];
                if (go > max){
                    max = go;
                    maxIndex = i + index;
                }
            }
            index = maxIndex;
            distance = len - 1 - index;
        }
        return true;
    }
}
